package com.bocek.chakmapoint.view.sort;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

import com.bocek.chakmapoint.controller.SlideSortController;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.FlowLayout;

public class SlideThumbnail extends JPanel {
	private static final long serialVersionUID = -8304840420570154606L;
	private SlideThumbnail self = this;
	private JPanel container;

	/**
	 * Create the panel.
	 */
	public SlideThumbnail(JPanel container, String title) {
		this.container = container;
		
		setFocusable(true);
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBackground(Color.WHITE);
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblIndex = new JLabel(title);
		add(lblIndex);
		
		addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				requestFocus();
				SlideSortView.getInstance().setSelectedIndex(getSelfIndex());
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});
	}
	
	public int getSelfIndex() {
		Component[] components = container.getComponents();
		
		for (int  i = 0; i < components.length; i++) {
			if (components[i] == self) return i + 1;
		}
		
		return -1;
	}

}
